# -*- coding: utf-8 -*-
#-----------------------
# @Time ： 2021/4/9 14:42
# @Author ： liyi03
# @File ：handleYaml.py
# @IDE ：PyCharm
#-----------------------
import yaml,os

class YamlHandler:

    def __init__(self,file,encoding = 'utf-8'):
         self.file = file
         self.encoding = encoding

    #读取yaml数据
    def get_yaml_data(self):
        with open(self.file,encoding=self.encoding) as f:
            data = yaml.safe_load(f.read())
        print('读取出来的yaml数据：\n',data)
        return data

    #写入yaml数据
    def write_yaml(self,data):
        """
        有中文需要添加allow_unicode=True
        :param data:
        :param data:
        :return:
        """
        with open(self.file,'w',encoding=self.encoding) as f:
            yaml.dump(data,stream=f,allow_unicode = True)



if __name__ == '__main__':
    base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))).replace("\\", '/')  # 当前项目的根目录
    home_page_nine_grid_swipe_path = os.path.join(base_path,'testData/commonData' , 'home_page_nine_grid_swipe.yaml').replace("\\", '/')
    # yaml_path = os.path.join(base_path,'testData','write_demos.yaml').replace("\\", '/')
    # print(yaml_path)
    # print(YamlHandler(yaml_path).get_yaml_data())

    data = {"x1":10,"y1":20,"x2":30,"y2":40}
    YamlHandler(home_page_nine_grid_swipe_path).write_yaml(data)
